#include "Edge.h"
#include<cmath>

Edge::Edge(Vertex* start,Vertex* end,QObject* parent):
    QObject(parent),
    QGraphicsLineItem(start->point.x(),start->point.y(),end->point.x(),end->point.y()),
    start(start),
    end(end)
{
}

void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QLineF line(start->point,end->point);
    QPen pen(QColor(125,185,222));
    pen.setWidth(2);
    painter->setPen(pen);
    painter->setRenderHint(QPainter::HighQualityAntialiasing);
    painter->drawLine(line);
    painter->setFont(TagFont);
    painter->setPen(Qt::black);
    painter->drawText(QPointF((start->point.x()+end->point.x())/2,(start->point.y()+end->point.y())/2),QString::number(Z,'f',2));
    painter->setPen(pen);
    qreal x1=end->point.x();
    qreal y1=end->point.y();
    qreal x2=start->point.x();
    qreal y2=start->point.y();
    qreal x3=x1-10*cos(atan2(y2-y1,x1-x2)-0.5);
    qreal y3=y1+10*sin(atan2(y2-y1,x1-x2)-0.5);
    qreal x4=x1-10*cos(atan2(y2-y1,x1-x2)+0.5);
    qreal y4=y1+10*sin(atan2(y2-y1,x1-x2)+0.5);
    painter->drawLine(x1,y1,x3,y3);
    painter->drawLine(x1,y1,x4,y4);
}

QRectF Edge::boundingRect() const{
    QRectF rect=QGraphicsLineItem::boundingRect();
    QFontMetrics fm(TagFont);
    rect=rect.united(fm.boundingRect(QString::number(Z,'f',2)));
    return rect;
}
